﻿Imports System.Data.OleDb

Public Class ProfileViewer
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            If (Not Page.IsPostBack) Then
                If (Request.QueryString("ProfileId") <> Nothing) Then
                    addVisitor()


                    Dim getProfile As ProfilesManagement = New ProfilesManagement()

                    Dim dr As OleDbDataReader = getProfile.SelectProfileByProfileID(Convert.ToInt32(Request.QueryString("ProfileId")))

                    If dr.HasRows Then
                        dr.Read()
                        If Not IsDBNull(dr("ProfileName")) Then lblName.Text = (dr("ProfileName"))
                        If Not IsDBNull(dr("ProfileSurname")) Then lblSurname.Text = (dr("ProfileSurname"))
                        If Not IsDBNull(dr("ProfileHobbies")) Then lblHobbies.Text = (dr("ProfileHobbies"))
                        If Not IsDBNull(dr("ProfileDob")) Then lblBirth.Text = (dr("ProfileDob"))
                        If Not IsDBNull(dr("ProfileAvatar")) Then imgAvatar.ImageUrl = (dr("ProfileAvatar"))
                        If Not IsDBNull(dr("UserID")) Then Session("ProfileUserId") = (dr("UserID"))


                    End If

                    Dim getPointsAwarded As Users_RankingPointsManagement = New Users_RankingPointsManagement()
                    Dim dr0 As OleDbDataReader = getPointsAwarded.SelectSumRankingPointsByUserId(Convert.ToInt32(Session("ProfileUserId")))
                    grvPointsAwarded.DataSource = dr0
                    grvPointsAwarded.DataBind()


                    Dim selectTotalRank As Users_RankingPointsManagement = New Users_RankingPointsManagement()
                    Dim dr2 As OleDbDataReader = selectTotalRank.SelectSumRankingPointsTotalByUserId(Convert.ToInt32(Session("ProfileUserId")))

                    If dr2.HasRows Then
                        dr2.Read()
                        If Not IsDBNull(dr2("TotalPoints")) Then lblTotalPoints.Text = (dr2("TotalPoints"))
                        If (lblTotalPoints.Text = "Label") Then
                            lblTotalPoints.Text = "0"
                        End If
                    Else
                        lblTotalPoints.Text = "0"
                    End If

                    Dim selectAverage As QuizGameTimeManagement = New QuizGameTimeManagement()
                    Dim dr3 As OleDbDataReader = selectAverage.SelectAverageTimeTakenByUser(Convert.ToInt32(Session("ProfileUserId")))

                    If dr3.HasRows Then
                        dr3.Read()
                        If Not IsDBNull(dr3("AverageTimeTaken")) Then lblAverageTimeTaken.Text = (dr3("AverageTimeTaken")).ToString() + " seconds"
                    Else
                        lblAverageTimeTaken.Text = "None"
                    End If

                    Dim userId As Int32 = New aspnet_UsersManagement().SelectUserIDByUsername(User.Identity.Name)
                    If (userId = Convert.ToInt32(Session("ProfileUserId"))) Then
                        btnEditProfile.Visible = True
                    Else
                        btnEditProfile.Visible = False

                    End If

                End If
            End If

        Catch ex As Exception

        End Try
    End Sub

    Protected Function getQuizImage(ByVal quizId As String)
        Try

            Dim getImage As QuizManagement = New QuizManagement()


            Dim dr As OleDbDataReader = getImage.SelectQuizByQuizId(Convert.ToInt32(quizId))
            Dim quizPicture As String = ""
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("QuizPicture")) Then quizPicture = (dr("QuizPicture"))
            End If

            Return quizPicture

        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Protected Function getQuizTitle(ByVal quizId As String)
        Try


            Dim getImage As QuizManagement = New QuizManagement()

            Dim dr As OleDbDataReader = getImage.SelectQuizByQuizId(Convert.ToInt32(quizId))

            Dim quizTitle As String = ""
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("QuizName")) Then quizTitle = (dr("QuizName"))
            End If

            Return quizTitle
        Catch ex As Exception
            Return Nothing
        End Try
    End Function

    Private Sub addVisitor()
        Try


            Dim aCookie As HttpCookie = Request.Cookies("userIdCookie")
            Dim userId = Convert.ToInt32(aCookie.Value)

            Dim pageName As String = Request.Url.PathAndQuery
            pageName = pageName.Replace("/", "")

            Dim getPageNameId As PagesNamesManagement = New PagesNamesManagement()
            Dim dr As OleDbDataReader = getPageNameId.SelectPagesNamesByPageNameContent(pageName)

            Dim pageId As Int32 = -1

            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("PageNameId")) Then pageId = Convert.ToInt32(dr("PageNameId"))
            End If


            If (pageId <> -1) Then

                Dim insertUser As User_PagesNameManagement = New User_PagesNameManagement()
                insertUser.InsertUserPageName(userId, pageId)

            End If
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub btnEditProfile_Click(sender As Object, e As EventArgs) Handles btnEditProfile.Click
        Try

            Response.Redirect("~/ProfileManager.aspx")

        Catch ex As Exception

        End Try
    End Sub
End Class